#!/bin/sh
# 简单的cd 信息管理

cat  << !MANUAL!
this is a music manager
modify by ziqtang
hello everybody!!
!MANUAL!

# 设置全局的变量
menu_choice=""
current_cd=""
title_file="title.cdb"
tracks_file="tracks.cdb"
temp_file=/tmp/cdb.$$    ## $$代表的是当前进程号
trap 'rm -f $temp_file' EXIT

# 返回
get_return() {
    echo  "Press return \c"    #\c转义，代表输出不换行
    read x
    return 0
}

# 确认询问
get_confirm() {
    echo "Are you sure(y/n): \c"
    while true
    do
        read x
        case "$x" in
            y | yes | Y | Yes | YES )
                return 0;;
            n | no | N | No | NO )
            echo
            echo "Cancelled"
            return 1;;
            *) echo "please enter yes or no(y/n):";;
        esac
    done
}

set_menu_choice() {
    clear  #清除屏幕上的输出
    echo "Options :-"
    echo
    echo "  a) Add new cd"
    echo "  f) Find cd"
    echo "  c) Count the CDs and tracks in the catalog"
    if [ "$cdcatnum" != "" ];then
        echo "   l) List tracks on $cdtitle"
        echo "   r) Remove $cdtitle"
        echo "   u) Update track info for $cdtitle"
    fi
    echo "  q) Quit"
    echo
    echo "Plase enter choice the press return: \c"
    read menu_choice  #键盘上的输入赋值给该变量
    return
}

insert_title() {
    echo $* >> $title_file  #所有参数写入文件中 
    return
}

insert_track() {
    echo $* >> $tracks_file
    return
}

add_record_tracks() {
    echo "Enter track info for this cd"
    echo "when no more tracks enter q"
    cdtrack=1
    cdttitle=""
    while [ "$cdttitle" != "q" ]
    do 
         echo "Track $cdtrack,track title? \c"
         read tmp
         #${$tmp%% *}的意思就是从tmp这个变量的值中，从后面开始以最长匹配删去%%后面的表达式内容。
         cdttitle=${tmp%%,*}  #这个做法是去除，后面的东西，防止和默认存在文档中的，号冲突
         if [ "$tmp" != "$cdttitle" ]; then
             echo "Sorry,no commas allowed"
             continue
         fi
         if [ -n "$cdttitle" ]; then
             if [ "$cdttitle" != "q" ]; then
                 insert_track $cdcatnum,$cdtrack,$cdttitle
             fi
         else
             cdtrack=$((cdtrack-1))  #求值
         fi
         cdtrack=$((cdtrack+1))
     done
}

add_records(){
    # Prompt for the initial information

    echo "Enter catalog name: \c"
    read tmp
    cdcatnum=${tmp%%,*}   ##从尾部开始最长匹配，*删除

    echo "Enter title: \c"
    read tmp
    cdtitle=${tmp%%,*}

    echo "Enter type: \c"
    read tmp
    cdtype=${tmp%%,*}

    echo  "Enter artist/composer: \c"
    read tmp
    cdac=${tmp%%,*}


    # Check that they want to enter the information

    echo About to add new entry
    echo "$cdcatnum $cdtitle $cdtype $cdac"

    # If confirmed then append it to the titles file

    if get_confirm; then   #返回值为0才是正确
        insert_title $cdcatnum,$cdtitle,$cdtype,$cdac
        add_record_tracks
    else
        remove_records
    fi
    return
}

# 这个函数的作用是使用grep命令在CD唱片标题文件中查找CD唱片的有关资料。
# 因为grep的返回值看之能告诉你是匹配了0次还是多次，所以把grep命令的输出保存在一个临时文件，
# 文件中的每行对应一次匹配，然后在统计该文件的行数。
# 单词统计命令wc在其输出中使用空格符分隔被统计文件中的行数、单词数和字符个数。

find_cd(){
    if [ "$1" = "n" ]; then
        asklist=n
    else
        asklist=y
    fi
    cdcatnum=""
    echo  "Enter a string to search for in the CD titles: \c"
    read searchstr
    if [ "$searchstr" = "" ]; then
        return 0
    fi

    grep "$searchstr" $title_file > $temp_file  #搜索到的内容写到temp_file

    set $(wc -l $temp_file)    #wc 是计数命令
    linesfound=$1

    case "$linesfound" in
        0)   echo "Sorry, nothing found"
            get_return
            return 0
            ;;
        1)  ;;
        2)  echo "Sorry, not unique."
            echo "Found the following"
            cat $temp_file
            get_return
            return 0
    esac

    IFS=","
    read cdcatnum cdtitle cdtype cdac < $temp_file
    IFS=" "

    if [ -z "$cdcatnum" ]; then
        echo "Sorry,could not extract catalog field from $temp_file"
        get_return
        return 0
    fi

    echo
    echo Catalog number: $cdcatnum
    echo Title: $cdtitle
    echo Type: $cdtype
    echo Artist/Composer: $cdac
    echo
    get_return

    if [ "$asklist" = "y" ]; then
        echo  "View tracks for this CD(y/n)? \c"
        read x
        if [ "$x" = "y" ]; then
            echo
            list_tracks
            echo
        fi
    fi
    return 1
}


# 这个函数用于重新输入CD唱片的资料

update_cd() {
    if [ -z "$cdcatnum" ]; then
        echo "You must select a CD first"
        find_cd n
    fi
    if [ -n "$cdcatnum" ]; then
        echo "Current tracks are :-"
        list_tracks
        echo
        echo "This will re enter the tracks for $cdtitle"
        get_confirm && {
        grep -v "^${cdcatnum}," $tracks_file > $temp_file
        mv $temp_file $tracks_file
        echo
        add_record_tracks
    }
    fi
    return
}


# 统计数据库中CD唱片个数和曲目个数
count_cds() {
    set $(wc -l < $title_file)
    num_titles=$1
    set $(wc -l < $tracks_file)
    num_tracks=$1
    echo found $num_titles CDs, with a total of $num_tracks tracks
    get_return
    return
}


# 这个函数用于从数据库文件中删除数据项，通过grep -v实现
# 必须使用一个临时文件来完成这一工作

remove_records() {
    if [ -z "$cdcatnum" ]; then
        echo You must select a CD first
        find_cd n
    fi
    if [ -n "$cdcatnum" ]; then
        echo "You are about to delete $cdtitle"
        get_confirm && {
        grep -v "^${cdcatnum}," $title_file > $temp_file
        mv $temp_file < $title_file
        grep -v "^${cdcatnum}," $tracks_file > $temp_file
        mv $temp_file $tracks_file
        cdcatnum=""
        echo Entry removed
    }
    get_return
    fi
    return
}


# 这个函数使用grep命令查找行，通过cut命令访问你想要的字段，再通过more命令按页输出。

list_tracks() {
    if [ "$cdcatnum" = "" ]; then
        echo no CD selected yet
        return
    else
        grep "^${cdcatnum}," $tracks_file > $temp_file
        num_tracks=$(wc -l $temp_file)
        if [ "$num_tracks" = "0" ]; then
            echo no tracks found for $cdtitle
        else {
            echo
            echo "$cdtitle :-"
            echo
            cut -f 2- -d, $temp_file
            echo
        } | ${PAGER:-more}
        fi
    fi
    get_return
    return
}



# 主程序部分
rm -f $temp_file
if [ ! -f < $title_file ]; then  #不存在文集则新建
    touch < $title_file
fi
if [ ! -f $tracks_file ]; then 
    touch $tracks_file
fi

# Now the application proper

clear
echo
echo
echo "Mini CD manager"
sleep 1

quit=n
while [ "$quit" != "y" ];
do
    set_menu_choice
    case "$menu_choice" in
        a) add_records;;
        r) remove_records;;
        f) find_cd y;;
        u) update_cd;;
        c) count_cds;;
        l) list_tracks;;
        b)
            echo
            more < $title_file
            echo
            get_return;;
        q | Q )
            quit=y;;
        *) echo "Sorry, choice not recognized";;
    esac
done

#Tidy up and leave

if [ -f $temp_file ]; then  #最后删除临时文件:
    rm -f $temp_file
fi
echo "Finished"
exit 0



